﻿using Iworld.Backstage.Framework;
using Iworld.Backstage.Framework.AdministratorService;
using Iworld.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage
{
    [Pop(Pop.修改自身密码)]
    public partial class EditSelfPasswordWindow : ChildWindow,IPop
    {
        public EditSelfPasswordWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enter(object sender, RoutedEventArgs e)
        {
            if (UINewPassword.Password != UINewPasswordConfirm.Password)
            {
                ShowMsg("两次密码输入不一致");
                return;
            }
            ConfirmPrompt cp = new ConfirmPrompt() { Info ="确认修改吗?"};
            cp.Closed += (s, args) => { 
               if(cp.DialogResult==true)
               {
                   AdministratorServiceClient client = new AdministratorServiceClient();
                   client.EditPasswordCompleted += client_EditPasswordCompleted;
                   client.EditPasswordAsync(new EditPasswordImport() { 
                    NewPassword = this.UINewPassword.Password
                    , OldPassword=this.UIOldPassword.Password
                    ,
                    Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
                   });
               }
            };
            cp.Show();
        }

        void client_EditPasswordCompleted(object sender, EditPasswordCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
                ShowMsg("修改成功");
                this.DialogResult = true;
            }
            else
            {
                ShowMsg(e.Result.Error);
            }
        }

        void ShowMsg(string msg)
        {
            new ErrorPrompt() { Info = msg }.Show();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

